﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace Soundteller.Common.Tests
{
    public static class ExceptionAssertionExtensions
    {
        public static void VerifyArgumentException(this Exception exception, string paramName)
        {
            Assert.IsNotNull(exception, "No exception thrown. Expected ArgumentException with paremname '{0}'", paramName);
            Assert.IsInstanceOfType(exception, typeof(ArgumentException), "Exception is not a ArgumentNullException. Expected ArgumentException with paramName '{0}'", paramName);

            var argumentNullException = exception as ArgumentException;
            Assert.AreEqual(paramName, argumentNullException.ParamName, "ArgumentException has wrong paremname. Expected ArgumentException with code '{0}'. Actual: '{1}'", paramName, argumentNullException.ParamName);
        }

        public static void VerifyArgumentNullException(this Exception exception, string paramName)
        {
            Assert.IsNotNull(exception, "No exception thrown. Expected ArgumentNullException with paremname '{0}'", paramName);
            Assert.IsInstanceOfType(exception, typeof(ArgumentNullException), "Exception is not a ArgumentNullException. Expected ArgumentNullException with paramName '{0}'", paramName);

            var argumentNullException = exception as ArgumentNullException;
            Assert.AreEqual(paramName, argumentNullException.ParamName, "ArgumentNullException has wrong paremname. Expected ArgumentNullException with code '{0}'. Actual: '{1}'", paramName, argumentNullException.ParamName);
        }

        public static void VerifyInvalidOperationException(this Exception exception, string message)
        {
            Assert.IsNotNull(exception, "No exception thrown. Expected InvalidOperationException with message '{0}'", message);
            Assert.IsInstanceOfType(exception, typeof(InvalidOperationException), "Exception is not a InvalidOperationException. Expected InvalidOperationException with message '{0}'", message);

            var operationException = exception as InvalidOperationException;
            Assert.AreEqual(message, operationException.Message, "InvalidOperationException has wrong message. Expected InvalidOperationException with message '{0}'. Actual: '{1}'", message, operationException.Message);
        }

        public static void VerifyKeyNotFoundException(this Exception exception)
        {
            Assert.IsNotNull(exception, "No exception thrown. Expected KeyNotFoundException");
            Assert.IsInstanceOfType(exception, typeof(KeyNotFoundException), "Exception is not a KeyNotFoundException. Expected KeyNotFoundException");
        }
    }
}
